package com.wuxiaolong.backend.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuxiaolong.backend.constant.Constants;
import com.wuxiaolong.backend.entity.UserEntity;
import com.wuxiaolong.backend.mapper.UserEntityMapper;
import com.wuxiaolong.backend.model.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 *
 * @author Leo
 * @date 2020-04-20
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserEntityMapper userEntityMapper;

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public JsonResult<PageInfo<UserEntity>> getUsers(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "username", required = false) String username) {

        // 分页设置 一定要放在最上面
        if(pageNum == null || pageNum < 0){
            pageNum = Constants.DEFAULT_PAGE_NUM;
        }
        if(pageSize == null || pageSize < 0){
            pageSize = Constants.DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(pageNum, pageSize, true);

        // 查询列表数据
        UserEntity param = new UserEntity();
        param.setUsername(username);
        List<UserEntity> list = userEntityMapper.selectListByDynamicConditionsAndPage(param);

        // 获得分页数据
        PageInfo<UserEntity> page = new PageInfo<UserEntity>(list);

        return new JsonResult<PageInfo<UserEntity>>(page);
    }
}
